package com.oocl.zero.datahandle.service.impl;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Set;

import com.oocl.zero.datahandle.dao.CustomerDao;
import com.oocl.zero.datahandle.dao.impl.CustomerDaoImpl;
import com.oocl.zero.datahandle.entity.Customer;
import com.oocl.zero.datahandle.service.CustomerService;
import com.oocl.zero.datahandle.util.CustomerPrinter;

public class CustomerServiceImpl implements CustomerService{

	private CustomerDao customerDao=new CustomerDaoImpl();
	
	@Override
	public void handle(String command, OutputStream out) {
		if(command==null)
			return;
		switch (command) {
		case "L#xml#id#":
			sortByIdPrintInXmlStyle(out);
			break;
		case "L#xml#birth#":
			sortByBirthPrintInXmlStyle(out);
			break;
		case "L#line#id#":
			sortByIdPrintInLineStyle(out);
			break;
		case "L#line#birth#":
			sortByBirthPrintInLineStyle(out);
			break;
			
		case "C#xml#id#":
			sortByIdPrintInXmlStyleFile();
			break;
		case "C#xml#birth#":
			sortByBirthPrintInXmlStyleFile();
			break;
		case "C#line#id#":
			sortByIdPrintInLineStyleFile();
			break;
		case "C#line#birth#":
			sortByBirthPrintInLineStyleFile();
			break;
		default:
			break;
		}
	}

	private void sortByBirthPrintInLineStyleFile() {
		OutputStream out;
		try {
			out = new FileOutputStream("line#birth#.txt");
			sortByBirthPrintInLineStyle( out);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void sortByIdPrintInLineStyleFile() {
		OutputStream out;
		try {
			out = new FileOutputStream("line#id#.txt");
			sortByIdPrintInLineStyle( out);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void sortByBirthPrintInXmlStyleFile() {
		OutputStream out;
		try {
			out = new FileOutputStream("xml#birth#.txt");
			sortByBirthPrintInXmlStyle( out);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void sortByIdPrintInXmlStyleFile() {
		OutputStream out;
		try {
			out = new FileOutputStream("xml#id#.txt");
			sortByIdPrintInXmlStyle( out);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void sortByBirthPrintInLineStyle(OutputStream out) {
		Set<Customer> set=customerDao.sortByBirth();
		try {
			StringBuilder buf=CustomerPrinter.getLineStyle(set);
			
			BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out));
			
			writer.write(buf.toString());
			writer.newLine();
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void sortByIdPrintInLineStyle(OutputStream out) {
		Set<Customer> set=customerDao.sortById();
		try {
			StringBuilder buf=CustomerPrinter.getLineStyle(set);
			
			BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out));
			
			writer.write(buf.toString());
			writer.newLine();
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void sortByBirthPrintInXmlStyle(OutputStream out) {
		Set<Customer> set=customerDao.sortByBirth();
		try {
			for (Customer c : set) {
				StringBuilder buf=CustomerPrinter.getXmlStyle(c);
				
				BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out));
				
				writer.write(buf.toString());
				writer.newLine();
				writer.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void sortByIdPrintInXmlStyle(OutputStream out) {
		Set<Customer> set=customerDao.sortById();
		try {
			for (Customer c : set) {
				StringBuilder buf=CustomerPrinter.getXmlStyle(c);
				
				BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out));
				
				writer.write(buf.toString());
				writer.newLine();
				writer.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
